package com.taijihuabao.common.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author changhui.tu
 */
@Configuration
public class LoginInterceptor implements WebMvcConfigurer {

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //创建拦截器对象，过滤所有
        registry.addInterceptor(getTokenInter()).addPathPatterns("/**")
                //放行登录注册
                .excludePathPatterns("/**/login").excludePathPatterns("/**/register")
                //放行注册、登录验证码
                .excludePathPatterns("/**/registerVailCode").excludePathPatterns("/**/loginVailCode")
                //放行重置密码邮件、重置密码验证码校验、密码重置
                .excludePathPatterns("/**/repeatPassVailCode").excludePathPatterns("/**/repeatPassVerify")
                .excludePathPatterns("/**/repeatPass").excludePathPatterns("/profile/**")
                //放行swagger
                .excludePathPatterns("/swagger-resources/**").excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns("/webjars/**").excludePathPatterns("/v2/**");
    }

    /**
     * 添加虚拟路径
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/profile/**").addResourceLocations("file:/root/images/profiles/");
    }

    @Bean
    public TokenInter getTokenInter() {
        return new TokenInter();
    }

}
